/**
 * 
 */
package org.apache.struts2.gtgrid;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.apache.struts2.gtgrid.model.GtColumnInfo;
import org.apache.struts2.gtgrid.model.GtFilterInfo;
import org.apache.struts2.gtgrid.model.GtGridInfo;
import org.apache.struts2.gtgrid.model.GtPageInfo;
import org.apache.struts2.gtgrid.model.GtSortInfo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * gt-grid base action,其他gt-grid的action需继承此类<br/>
 * <pre>
 * 以下方法需toJSON(struts2 json plugin):
 * {@link #getPageInfo()}
 * {@link #getPageData()}
 * {@link #getException()}
 * {@link #isSuccess()}
 * 
 * </pre>
 * @author Stevie
 * @since 2009-12-4
 * @version 1.0
 * 
 */
@SuppressWarnings("unchecked")
public abstract class GtGridBaseAction<T> extends ActionSupport implements
		GtGridAware<T> {
	private static final long serialVersionUID = 6822120115149949591L;

	protected GtGridInfo gridInfo = new GtGridInfo();
	protected GtPageInfo pageInfo = new GtPageInfo();
	protected List<GtSortInfo> sortInfos = new ArrayList<GtSortInfo>(1);
	protected List<GtFilterInfo> filterInfos = new ArrayList<GtFilterInfo>(1);
	protected List<GtColumnInfo> columnInfos = new ArrayList<GtColumnInfo>(10);
	protected List<T> insertedRecords = new ArrayList<T>(1);
	protected List<T> updatedRecords = new ArrayList<T>(1);
	protected List<T> deletedRecords = new ArrayList<T>(5);
	
	protected List<T> pageData = new ArrayList<T>(20);
	
	protected Map parameterMap = null;
	protected String exception;
	protected boolean success;

	
	/**
	 * gt-grid bean Class<T>
	 * @return
	 */
	public abstract Class<T> getBeanClass();
	
	/**
	 * @JSON(name = "pageInfo")
	 */
	public abstract GtPageInfo getPageInfo();
	
	/**
	 * @JSON(name = "data")
	 * @return
	 */
	public abstract List<T> getPageData();
	
	/**
	 * @JSON(name="exception")
	 * return exception
	 */
	public abstract String getException();
	

	/**
	 * 	@JSON(name="success")
	 *	return success
	 */
	public abstract boolean isSuccess();

	
	public void setParameterMap(Map parameterMap) {
		this.parameterMap = parameterMap;
	}

	public String[] getParameterValues(String name) {
		return (String[]) parameterMap.get(name);
	}

	public String getParameter(String name) {
		String[] pv = getParameterValues(name);
		if (pv != null && pv.length > 0) {
			return pv[0];
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.gt_grid.GtGridAware#setColumnInfos(java.util.List)
	 */
	public void setColumnInfos(List<GtColumnInfo> columnInfos) {
		this.columnInfos = columnInfos;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.gt_grid.GtGridAware#setFilterInfos(java.util.List)
	 */
	public void setFilterInfos(List<GtFilterInfo> filterInfos) {
		this.filterInfos = filterInfos;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.gt_grid.GtGridAware#setGridInfo(org.apache.struts2
	 * .gt_grid.model.GtGridInfo)
	 */
	public void setGridInfo(GtGridInfo gridInfo) {
		this.gridInfo = gridInfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.gt_grid.GtGridAware#setPageInfo(org.apache.struts2
	 * .gt_grid.model.GtPageInfo)
	 */
	public void setPageInfo(GtPageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.gt_grid.GtGridAware#setSortInfos(java.util.List)
	 */
	public void setSortInfos(List<GtSortInfo> sortInfos) {
		this.sortInfos = sortInfos;
	}

	public void setInsertedRecords(JSONArray insertedRecords) {
		this.insertedRecords = toJava(insertedRecords);
	}

	public void setUpdatedRecords(JSONArray updatedRecords) {
		this.updatedRecords = toJava(updatedRecords);
	}

	public void setDeletedRecords(JSONArray deletedRecords) {
		this.deletedRecords = toJava(deletedRecords);
	}

	/**
	 * 将jsonArray转换成相应对象
	 * 
	 * @param jsonArray
	 * @return
	 */
	protected List<T> toJava(JSONArray jsonArray) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(getBeanClass());
		return (List<T>) JSONSerializer.toJava(jsonArray, jsonConfig);
	}

	public void setException(String exception) {
		this.exception = exception;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public void setPageData(List<T> pageData) {
		this.pageData = pageData;
	}
}
